package com.jn.okhttp.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.jn.okhttp.common.WeakRrfHandler;
import com.jn.okhttp.contract.QAndAContract;
import com.jn.okhttp.entity.QAndAEntity;
import com.jn.okhttp.utils.LogUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class QAndAPresenter implements QAndAContract.IQAndAPresenter {
    public static final int HTTP_SUCCESS = 0;
    public static final int HTTP_FAIL = 1;
    private Context mContext;
    private QAndAContract.IQAndAView mQAndAView;
    private OkHttpClient mClient;

    public QAndAPresenter(QAndAContract.IQAndAView qAndAView, Context context) {
        mQAndAView = qAndAView;
        mContext = context;
        mClient = new OkHttpClient();
    }

    @Override
    public void getData() {//get方式异步请求
        String type = "json";
        Request request = new Request.Builder()
                .url("https://shanhe.kim/api/za/shenhuifu.php?type=" + type)
                //.get()
                .build();
        Call call = mClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                LogUtils.d("asdf", "onFailure");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String body = response.body().string();
                String url = call.request().url().toString();
                LogUtils.d("asdf", "url : " + url);
                LogUtils.d("asdf", "onResponse : " + body);
                Message message = Message.obtain();
                message.obj = new String[]{body, url};
                message.what = HTTP_SUCCESS;
                mHandler.sendMessage(message);
            }
        });
    }

    WeakRrfHandler<QAndAPresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int result = msg.what;
            switch (result) {
                case HTTP_SUCCESS:
                    String[] data = (String[]) msg.obj;
                    if (data == null || data.length < 2) {
                        return false;
                    }
                    String jsonStr = data[0];
                    Gson gson = new Gson();
                    QAndAEntity qandAEntity = gson.fromJson(jsonStr, QAndAEntity.class);
                    if (mQAndAView != null) {
                        mQAndAView.showData(qandAEntity);
                    }
                    break;
                case HTTP_FAIL:
                    break;
            }
            return false;
        }
    });
}